
#import "WebView.h"
#import <WebKit/WebKit.h>
@interface WebView()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webview;
@property (nonatomic, strong) UIActivityIndicatorView *hudView;
@end
@implementation WebView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    return  self;
}
- (void)show:(NSString *)str{
    NSString *url  = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"GET";
    [self.webview loadRequest:request];
    [self.hudView startAnimating];
    self.webview.scrollView.bouncesZoom = NO;
    if (@available(iOS 11.0, *)) {
        self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }else{
        [self currentVC].automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (WKWebView *)webview{
    if (!_webview) {
        _webview = [WKWebView new];
        [self addSubview:_webview];
        _webview.navigationDelegate = self;
        _webview.translatesAutoresizingMaskIntoConstraints = NO;
        NSLayoutConstraint *top = [_webview a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeTop c:0];
        NSLayoutConstraint *left = [_webview a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeLeft c:0];
        NSLayoutConstraint *right = [_webview a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeRight c:0];
        NSLayoutConstraint *bottom = [_webview a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeBottom c:0];
        [self addConstraints:@[top,left,right,bottom]];
    }
    return _webview;
}
- (UIActivityIndicatorView *)hudView
{
    if (!_hudView)
    {
        _hudView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:0];
        _hudView.translatesAutoresizingMaskIntoConstraints = NO;
        _hudView.color = [UIColor colorHex:@"#000000" alpha:0.8];
        [self addSubview:_hudView];
        NSLayoutConstraint *top = [_hudView a1:NSLayoutAttributeCenterX e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeCenterX c:0];
        NSLayoutConstraint *left = [_hudView a1:NSLayoutAttributeCenterY e:NSLayoutRelationEqual t:self a2:NSLayoutAttributeCenterY c:0];
        NSLayoutConstraint *right = [_hudView a1:NSLayoutAttributeWidth e:NSLayoutRelationEqual t:nil a2:NSLayoutAttributeNotAnAttribute c:0];
        NSLayoutConstraint *bottom = [_hudView a1:NSLayoutAttributeHeight e:NSLayoutRelationEqual t:nil a2:NSLayoutAttributeNotAnAttribute c:0];
        [self addConstraints:@[top,left,right,bottom]];
    }
    return _hudView;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    [self.hudView startAnimating];
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [self.hudView stopAnimating];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [Toast startHud:LS(@"加载失败") stopTime:1 handle:nil];
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
    [self.hudView stopAnimating];
}

// 跳转失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    NSLog(@"");
}
@end
